/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RandomFilePanel.java
 *
 * Created on 30.01.2009, 19:59:40
 */
package jopensslgui;

import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author uli
 */
public class RandomFilePanel extends javax.swing.JPanel
{

    /** Creates new form RandomFilePanel */
    public RandomFilePanel()
    {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        outputFileLabel = new javax.swing.JLabel();
        randomOutputFileField = new javax.swing.JTextField();
        selectOutputFileButton1 = new javax.swing.JButton();
        sizeLabel = new javax.swing.JLabel();
        randomSizeSpinner = new javax.swing.JSpinner();
        randomSizeSuffixComboBox = new javax.swing.JComboBox();
        randomFileOKButton = new javax.swing.JButton();

        outputFileLabel.setText( i18n.getString("RandomFilePanel.outputFileLabel.text")); // NOI18N

        randomOutputFileField.setText( i18n.getString("RandomFilePanel.randomOutputFileField.text")); // NOI18N

        selectOutputFileButton1.setText(i18n.getString("RandomFilePanel.selectOutputFileButton1.text")); // NOI18N
        selectOutputFileButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectOutputFileButton1selectRandomOutputFileButtonActionPerformed(evt);
            }
        });

        sizeLabel.setText( i18n.getString("RandomFilePanel.sizeLabel.text")); // NOI18N

        randomSizeSuffixComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Bytes", "Kilobytes", "Megabytes", "Gigabytes" }));
        randomSizeSuffixComboBox.setSelectedIndex(1);

        randomFileOKButton.setText( i18n.getString("RandomFilePanel.randomFileOKButton.text")); // NOI18N
        randomFileOKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                randomFileOKButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(sizeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(outputFileLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                        .addGap(6, 6, 6)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(randomFileOKButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(randomOutputFileField, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(selectOutputFileButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(randomSizeSpinner, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(randomSizeSuffixComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(outputFileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(randomOutputFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectOutputFileButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(randomSizeSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sizeLabel)
                    .addComponent(randomSizeSuffixComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(randomFileOKButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void selectOutputFileButton1selectRandomOutputFileButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_selectOutputFileButton1selectRandomOutputFileButtonActionPerformed
    {//GEN-HEADEREND:event_selectOutputFileButton1selectRandomOutputFileButtonActionPerformed
        fileChooser.setSelectedFile(new File(randomOutputFileField.getText()));
        fileChooser.showSaveDialog(this);
        randomOutputFileField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}//GEN-LAST:event_selectOutputFileButton1selectRandomOutputFileButtonActionPerformed

    private void randomFileOKButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_randomFileOKButtonActionPerformed
    {//GEN-HEADEREND:event_randomFileOKButtonActionPerformed
        try
        {
            /**
             * First step: Check which size the file should have
             */
            SpinnerNumberModel sizeSpinnerModel =
                    (SpinnerNumberModel) randomSizeSpinner.getModel();
            long size = sizeSpinnerModel.getNumber().longValue();
            switch (randomSizeSuffixComboBox.getSelectedIndex())
            {
                case 1:
                //Kilobytes
                {
                    size *= 1024;
                    break;
                }
                case 2:
                //Megabytes
                {
                    size *= 1024 * 1024;
                    break;
                }
                case 3:
                //Gigabytes
                {
                    size *= 1024 * 1024 * 1024;
                    break;
                }
                default:
                    break; //case 0 --> bytes
            }
            /**
             * Second step: Generate the file
             */
            String outfileName = randomOutputFileField.getText();
            Runtime r = Runtime.getRuntime();
            Process p = r.exec("openssl rand -out " + outfileName + " " + Long.toString(size));
            p.waitFor();
            int exitValue = p.exitValue();
            if (exitValue == 0)
            {
                JOpenSSLFrame.displaySuccessMessage();
            }
            else
            {
                JOpenSSLFrame.displayErrorMessage(Integer.toString(exitValue));
            }
        }
        catch (InterruptedException ex)
        {
            Logger.getLogger(JOpenSSLFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex)
        {
            Logger.getLogger(JOpenSSLFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_randomFileOKButtonActionPerformed
    private JFileChooser fileChooser = new JFileChooser();
    private ResourceBundle i18n = ResourceBundle.getBundle("jopensslgui/Bundle"); //NOI18N
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel outputFileLabel;
    private javax.swing.JButton randomFileOKButton;
    private javax.swing.JTextField randomOutputFileField;
    private javax.swing.JSpinner randomSizeSpinner;
    private javax.swing.JComboBox randomSizeSuffixComboBox;
    private javax.swing.JButton selectOutputFileButton1;
    private javax.swing.JLabel sizeLabel;
    // End of variables declaration//GEN-END:variables
}
